/**
 * 检查url是否可以访问的jQuery插件
 * @author 祝文博
 * @date 2014-06-23 01:16:05
 */
define(function(require, exports, module) {
	
	var $ = require('jquery');
	
	function UrlCheck(inputSelect) {
		this.inputSelect = inputSelect;
	}
	
	module.exports = UrlCheck;
	
	UrlCheck.prototype.init = function() {
		$(document).on('blur', this.inputSelect, function(){
			var url = $(this).val().trim();
			var thisInput = $(this);
			statusLoading(thisInput);
			$.ajax({
				url : '/tqmmanagement/order/index.php?m=Operations&a=check_url_access',
				type : 'POST',
				dataType : 'json',
				data : {
					url : url
				},
				success : function (data) {
					if (data.errorCode == 0) {
						statusOk(thisInput);
					} else {
						statusError(thisInput);
					}
				},
				error : function () {
					statusError(thisInput);
				}
			});
		});
	}
	
	var statusLoading = function(input) {
		var tip = '<span class="checkUrlNotice"><img src="/tqmmanagement/frontapp/media/image/select2-spinner.gif"></span>';
		if (input.siblings('.checkUrlNotice')) {
			input.siblings('.checkUrlNotice').remove();
			input.after(tip);
		} else {
			input.after(tip);
		}
	}
	
	var statusOk = function(input) {
		var tip = '<span class="checkUrlNotice"><img src="/tqmmanagement/order/App/Tpl/Public/resources/images/icons/tick_circle.png"></span>';
		if (input.siblings('.checkUrlNotice')) {
			input.siblings('.checkUrlNotice').remove();
			input.after(tip);
		} else {
			input.after(tip);
		}
	}
	
	var statusError = function(input) {
		var tip = '<span class="checkUrlNotice"><img src="/tqmmanagement/order/App/Tpl/Public/resources/images/icons/cross_circle.png"></span>';
		if (input.siblings('.checkUrlNotice')) {
			input.siblings('.checkUrlNotice').remove();
			input.after(tip);
		} else {
			input.after(tip);
		}
	}
});